package com.us.user.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.opensymphony.xwork2.ActionContext;
import com.us.user.dao.IWebUserDAO;
import com.us.user.pojo.WebUser;
import com.us.util.MD5;

public class WebUserDAO extends SqlMapClientDaoSupport implements IWebUserDAO {
	private MD5 md5;
	public List getAllWebUser() {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("getAllWebUser");
	}

	public List searchAllUser(WebUser webUser) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("searchAllUser",webUser);
	}

	public WebUser webUserView(int user_id) {
		// TODO Auto-generated method stub
		return (WebUser) this.getSqlMapClientTemplate().queryForObject("webUserView", user_id);
	}

	public boolean webUserRegCheckName(String user_name) {
		// TODO Auto-generated method stub
		boolean flag = false;
		String id = (String) this.getSqlMapClientTemplate().queryForObject("webUserRegCheckName",user_name);
		if(id!=null)flag = true;
		return flag;
	}

	public String getUserIdByname(String user_name) {
		// TODO Auto-generated method stub
		return (String) this.getSqlMapClientTemplate().queryForObject("webUserRegCheckName",user_name);
	}

	public void webUserReg(WebUser webUser) {
		// TODO Auto-generated method stub
		Date date = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		String user_joindate = ft.format(date);
		webUser.setUser_joindate(user_joindate);
		webUser.setUser_password(md5.toencrypt(webUser.getUser_password()));
		this.getSqlMapClientTemplate().insert("insertWebUser",webUser);
	}

	public int UserLogin(WebUser webUser) {
		// TODO Auto-generated method stub
		webUser.setUser_password(md5.toencrypt(webUser.getUser_password()));
		int user_id = 0;
		String id = (String) this.getSqlMapClientTemplate().queryForObject("UserLogin", webUser);
		if(id==null){
			user_id = 0;
		}else {
			user_id = Integer.parseInt(id);
		}
		return user_id;
		}

	public boolean webUserRegCheckmail(String user_mail) {
		// TODO Auto-generated method stub
		boolean flag = false;
		String id = (String) this.getSqlMapClientTemplate().queryForObject("webUserRegCheckmail",user_mail);
		if(id!=null)flag = true;
		return flag;
	}

	public void delUserByid(String user_id) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("delwebUserByid",Integer.parseInt(user_id));
		this.getSqlMapClientTemplate().delete("delperUserByid",Integer.parseInt(user_id));
		this.getSqlMapClientTemplate().delete("delcomUserByid",Integer.parseInt(user_id));
	}

	public void webUserUpdate(WebUser webUser) {
		// TODO Auto-generated method stub
		if (("").equals(webUser.getUser_password())||webUser.getUser_password()==null) {
			this.getSqlMapClientTemplate().update("webUserUpdateNoPwd",webUser);
		}else{
			webUser.setUser_password(md5.toencrypt(webUser.getUser_password()));
			this.getSqlMapClientTemplate().update("webUserUpdatePwd",webUser);
		}
	}

	public boolean webUserEditCheckPsw(String oldpassword) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Map session = (Map) ActionContext.getContext().getSession();
		int user_id = ((WebUser) session.get("user")).getUser_id();
		String password = (String) this.getSqlMapClientTemplate().queryForObject("getPasswordBYid", user_id);
		if(!(password.equals(MD5.toencrypt(oldpassword)))){
			flag = true;
		}
		return flag;
	}

	public void updateWebUserMessage(WebUser webUser) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("updateWebUserMessage",webUser);
	}
	}

